package single_game.logic.race_simulator;

import java.util.ArrayList;
import java.util.Random;

import main.CyclingManager;
import single_game.logic.race_simulator.race_team.RaceRider;
import data.Cyclist;

public class RaceSimulatorOperations {
	
	private static Random random = new Random();
	private static ArrayList<Cyclist> cyclists = CyclingManager.getInstance().cyclists; 
	
	
	public static void setParameters(RaceRider actual, Group actualGroup, int pace, Boolean isRelay) {
		
		int myPace = pace + random.nextInt(10) - 5;
		double puls = actual.getPuls(); 
		double pulsLimit = getPulsLimit(actual, myPace, actualGroup);			
		
		if (isRelay) {
			if (actual == actualGroup.relayGroup.get(0))				 
				pulsLimit += 3.5;
			else if (actualGroup.inRelayGroup(actual))                    
				pulsLimit += 1.2;
		}		
		
		if (puls < pulsLimit && puls <= 200) {
			 actual.setPuls(puls+((pulsLimit-puls)*RaceSimulator.pulsFactor));
		}
		else if (puls > pulsLimit)
			actual.setPuls(puls-0.5);
		
		if (puls >= RaceSimulator.energyBoundPuls && actual.getEnergy() > RaceSimulator.energyMinimum)
			actual.setEnergy(actual.getEnergy()-((1.0/10000.0)*Math.pow(puls-155.0, 2.0)));
		else if (puls < RaceSimulator.energyBoundPuls && actual.getEnergy() < RaceSimulator.energyMaximum)
			actual.setEnergy(actual.getEnergy()+(double)(200-puls)/5000.0);
		
		actual.setStamina(actual.getStamina()-((puls-120)/(200 + RaceSimulator.cyclists.get(actual.getCyclistId()).getStamina()*10))*0.1);
		
		if ((actual.getEnergy() < RaceSimulator.energyMinimum && puls >= RaceSimulator.energyBoundPuls) && (!actual.getAction().equals("Stay Behind") && !actual.getAction().equals("Slow Down"))) {
			RaceSimulator.stayedBehind.add(actual);
			actual.setAction("Stay Behind");
		}			
	}
	
	
	public static double setPace(RaceRider rider, int terrain, Group group) {
		double pace;
		
		switch (terrain) {  
			case 0: pace = (double)(rider.getPower()*((double)(cyclists.get(rider.getCyclistId()).getMountain() / 10.0)))*0.15; 
					group.speed = 30.0 + (double)pace*0.25;   
					return pace;
			case 1: pace = (double)(rider.getPower()*((double)(cyclists.get(rider.getCyclistId()).getMountain() / 12.0)))*0.15; 
					group.speed = 25.0 + (double)pace*0.20;   
					return pace;
			case 2: pace = (double)(rider.getPower()*((double)(cyclists.get(rider.getCyclistId()).getMountain() / 10.0)))*0.15;
					group.speed = 18.0 + (double)pace*0.15;   
					return pace;
			case 3: pace = (double)(rider.getPower()*((double)(cyclists.get(rider.getCyclistId()).getMountain() / 10.0)))*0.16;
					group.speed = 15.0 + (double)pace*0.1;   
					return pace;
			case 4: pace = (double)(rider.getPower()*((double)(cyclists.get(rider.getCyclistId()).getMountain() / 10.0)))*0.18;
					group.speed = 12.0 + (double)pace*0.08;   
					return pace;
			case 5: pace = (double)(rider.getPower()*((double)(cyclists.get(rider.getCyclistId()).getMountain() / 10.0)))*0.20;
					group.speed = 9.0 + (double)pace*0.06;   
					return pace;
			case 6: pace = (double)(rider.getPower()*((double)(cyclists.get(rider.getCyclistId()).getDownhill() / 15.0)))*0.15;
					group.speed = 40.0 + (double)pace*0.12;   
					return pace;
			case 7: pace = (double)(rider.getPower()*((double)(cyclists.get(rider.getCyclistId()).getDownhill() / 10.0)))*0.12;
					group.speed = 45.0 + (double)pace*0.1;   
					return pace;
			case 8: pace = (double)(rider.getPower()*((double)(cyclists.get(rider.getCyclistId()).getDownhill() / 10.0)))*0.10;
					group.speed = 50.0 + (double)pace*0.06;   
					return pace;
			default: return 0;				
		}
	}
	
	
	public static double getPulsLimit(RaceRider rider, int myPace, Group group) {
		double pulsLimit; 
		Race race = Race.getInstance();
		
		switch (race.stage[(int)group.driven]) {
			case 0:
				pulsLimit = 120.0 + myPace / 3.2 + myPace/(((double)cyclists.get(rider.getCyclistId()).getFlat()-55)/5.2);
				break;
			case 3:
				pulsLimit = 120.0 + myPace / 3.2 + myPace/(((double)cyclists.get(rider.getCyclistId()).getMountain()-55)/6.2);
				break;
			case 4:
				pulsLimit = 120.0 + myPace / 3.2 + myPace/(((double)cyclists.get(rider.getCyclistId()).getMountain()-55)/6.6);
				break;
			default: 
				pulsLimit = 120.0 + myPace / 3.2 + myPace/(((double)cyclists.get(rider.getCyclistId()).getMountain()-55)/6.6);
		}
		return pulsLimit;
	}
	
	
	
	
}
